"""
获取所有的yaml文件,在将所有的文件内容读取出来,在将每个文件的内容进行解析
将文件解析出来
"""

import os

import yaml

from common.utils import root_path,ensure_path_sep



def get_all_files(file_path, yaml_data_switch=False) -> list:
    """
    获取文件路径
    :param file_path: 目录路径
    :param yaml_data_switch: 是否过滤文件为 yaml格式， True则过滤
    :return:
    """
    filename = []
    # 获取所有文件下的子文件名称
    for root, dirs, files in os.walk(file_path):
        for _file_path in files:
            path = os.path.join(root, _file_path)
            if yaml_data_switch:
                if 'yaml' in path or '.yml' in path:
                    filename.append(path)
            else:
                filename.append(path)
    return filename

def get_yaml_data(yaml_file) -> dict:
    """
    获取 yaml 中的数据
    :param: fileDir:
    :return: yaml文件读取后是一个列表字典
    """
    # 判断文件是否存在
    if os.path.exists(yaml_file):
        # file = ensure_path_sep(yaml_file)
        # print(f'yaml_file={yaml_file}')
        with open(yaml_file, 'r', encoding='utf-8') as f:
            yaml_data = yaml.load(f, Loader=yaml.FullLoader)
    else:
        raise FileNotFoundError("文件路径不存在")
    return yaml_data

class CaseDateCheck:

    def __init__(self):

        self.name = None
        self.is_run = None
        self.host = None
        self.url = None
        self.method = None
        self.header = None
        self.params = None
        self.body = None
        self.cookie = None
        self.validata = None

    def check_yaml_keys(self, yaml_file):
        """
        判断关键字缺少,有一个用例缺少的话,用例执行就中止,需要完善用例再执行
        """
        key_list = ['name', 'is_run', 'host', 'url', 'method', 'headers', 'params', 'body', 'cookie', 'validata']
        yaml_data = get_yaml_data(yaml_file)
        try:
            for yaml_dict in yaml_data:
                no_keys = []
                for k in key_list:
                    if k not in yaml_dict.keys():
                        no_keys.append(k)
                return print(f"{yaml_file}的 {yaml_dict.get('name')} ,用例缺少关键字:'{no_keys}',请检查")
        except:
            raise

    # def map_yaml_key_values(self, yaml_file):
    #     self.check_yaml_keys(yaml_file)
    #     yaml_data = get_yaml_data(yaml_file)
    #
    #     for yaml_dict in yaml_data:
    #         self.name = yaml_dict['name']
    #         self.host =





if __name__ == '__main__':
    file_list = get_all_files(root_path()+'/data_object')
    # print(file_list)
    # for i in file_list:
    #     d = get_yaml_data("i")
    #     print(d)
    case = CaseDateCheck()
    # case.check_yaml_keys(file_list[3])

    # print(get_yaml_data(file_list))
